package com.aat.dxfy.concurrent;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class ExecOKThread {

	public static void main(String[] args) throws InterruptedException, ExecutionException {
		ExecutorService threadPool=Executors.newCachedThreadPool();
		List<Callable<Integer>> tasks=new ArrayList<>();
		for(int i=0;i<10;i++){
			Callable<Integer> task=new Callable<Integer>() {
				public Integer call() throws Exception {
					int i=(int) (Math.random()*1000)+1;
					return i;
				}
			};
			tasks.add(task);
		}
		
		List<Future<Integer>>  futures=threadPool.invokeAll(tasks);
		for (Future<Integer> future : futures) {
			System.out.println(future.get());
		}
		
		threadPool.shutdown();
		
	}

}
